package com.situ.water_BE.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.util.getBeOrderNo;
import com.situ.water_BE.mapper.BeFlowMapper;
import com.situ.water_BE.mapper.BeFormulaMapper;
import com.situ.water_BE.mapper.BeOrderMapper;
import com.situ.water_BE.pojo.BeOrder;
import com.situ.water_BE.service.BeOrderService;
import com.situ.water_system.mapper.SyMetertypeMapper;
import com.situ.water_system.pojo.SyMetertype;
import com.situ.water_user.mapper.UsUserMapper;
import com.situ.water_user.pojo.UsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@Service
public class BeOrderServiceImpl extends ServiceImpl<BeOrderMapper, BeOrder> implements BeOrderService {
    @Autowired
    private BeOrderMapper dao;
    @Autowired
    private BeFlowMapper flowDao;
    @Autowired
    private BeFormulaMapper beFormulaDao;
    @Autowired
    private SyMetertypeMapper syMetertypeDao;
    @Autowired
    private UsUserMapper usUserDao;

    @Override
    public List<BeOrder> queryAll(String stepName) {
        return dao.queryAll(stepName);
    }

    @Transactional
    public int add(BeOrder beOrder, String orderId, int orderType, Integer sessionId) { //添加新用户
        beOrder.setOrderNo(orderId);
        beOrder.setStepID(2);
        beOrder.setCreateEmp(sessionId);
        beOrder.setCreateDate(new Date());
        beOrder.setLastEditEmp(sessionId);
        beOrder.setLastEditDate(new Date());
        beOrder.setOrderType(orderType);
        return dao.insert(beOrder);
    }

    @Override
    public int add2(BeOrder order, Integer sessionId) {
        order.setStepID(2);
        order.setLastEditEmp(sessionId);
        order.setLastEditDate(new Date());
        return dao.updateById(order);
    }

    @Override
    public String orderNo(String type) { //新用户查询数据库最后一行的工单号
        String s = dao.orderNo();
        return getBeOrderNo.orderNo(type, s);
    }

    @Override
    public int updateStepID(String id, Integer auditEmpID, String text, Integer stepID, Integer lastEditEmp, Date lastEditDate) {
        int i = dao.updateStepID(id, auditEmpID, text, stepID, lastEditEmp, lastEditDate);
        return i;
    }
    @Override
    public int updateStepID2(BeOrder beOrder,int sessionId) {
        beOrder.setStepID(-1);
        beOrder.setLastEditEmp(sessionId);
        beOrder.setLastEditDate(new Date());
        return dao.updateById(beOrder);
    }

    @Override
    public List<Map> gjQuery(BeOrder beOrder) {
        List<Map> map = dao.gjQuery(beOrder);
        return map;
    }
    @Override
    public int gjQueryCount(BeOrder beOrder) {

        return dao.gjQueryCount(beOrder);
    }

    @Override
    public Map queryId(String id) {
        Map map = dao.queryId(id);
        return map;
    }

    @Override
    public int updateStepID(String id, Integer stepID, Integer lastEditEmp, Date lastEditDate, Integer pd) {
        if (pd > 0) {
            return dao.updateStepID1(id, stepID, lastEditEmp, lastEditDate);//没有修改交施工费的日期
        } else {
            return dao.updateStepID2(id, stepID, lastEditEmp, lastEditDate);//修改了交施工费的日期
        }
    }

    @Override
    public Integer beFlowQueryId(String type, Integer buZou, int i) {
        if (i > 0 && i <= 10) {
            return flowDao.queryId(type, buZou);
        } else if (i > 10 && i <= 20) {
            return flowDao.queryIdJia(type, buZou);
        } else {
            return flowDao.queryIdJian(type, buZou);
        }
    }

    public List<SyMetertype> QuerySyMeterType() {
        return syMetertypeDao.selectList(null);
    }

    @Override
    public SyMetertype QueryIdSyMeterType(QueryWrapper<SyMetertype> queryWrapper) {
        return syMetertypeDao.selectOne(queryWrapper);
    }

    public BeOrder queryIdBeOrder(QueryWrapper<BeOrder> queryWrapper) {
        return dao.selectOne(queryWrapper);
    }

    @Override
    public String queryAreaId(String orNo) {
        return dao.queryAreaId(orNo);
    }

    @Override
    public UsUser userQueryId(QueryWrapper<UsUser> queryWrapper) {
        return usUserDao.selectOne(queryWrapper);
    }

    @Override
    public Map queryIdUserAndOrder(String orderNo) {
        return dao.queryIdUserAndOrder(orderNo);
    }

    @Override
    public UsUser queryUserUserNo(String userNo) {
        return usUserDao.selectById(userNo);
    }

    @Override
    public  List<Map> queryAllBeOrderUserSQL(Map<String,Object> map){
        return dao.queryBeOrderUserSQL(map);
    }

    @Override
    public int queryAllBeOrderUserSQLCount(Map<String, Object> map) {
        return dao.queryBeOrderUserSQLCount(map);
    }

    @Override
    public List<SyMetertype> queryAllSY_MeterType() {
        return syMetertypeDao.selectList(null);
    }

    @Override
    public List<Map> limitBeOrderSQL(){
        return dao.limitBeOrderSQL();
    }
}
